<?php
error_reporting(7);

$templatelist = "sorthome,subsortlist,subsortlistbit_level1,subsortlistbit_level2,subsortlistbit_level3,sorthome_articlelist,sorthome_articlelistbit_img,sorthome_articlelistbit,pagenav,sorthome_sortinfo,sorthome_sortinfo_image";
$templatelist .= ",hotarticlelist,hotarticlelistbit,poparticlelist,poparticlelistbit";
require "global.php";

if (!$sortinfo) {
        $sortinfo = validate_sortid($sortid);
} 

$sortinfo['description'] = str_replace("  ", "&nbsp;&nbsp;", nl2br($sortinfo['description']));

cachesorts();

$sortlist = makesortlist();

$navbar = makesortnavbar($sortid);

unset($sortinfos);
if ($sortinfo[showsortinfos] AND (pa_isset($sortinfo[img]) OR pa_isset($sortinfo[description]))) {
        if (pa_isset($sortinfo[img])) {
                eval("\$sortinfo[image] = \"" . gettemplate('sorthome_sortinfo_image') . "\";");
        } 
        eval("\$sortinfos = \"" . gettemplate('sorthome_sortinfo') . "\";");
} 
// mainsortlist
unset($subsortlist);
if (isset($subsort[$sortid])) {
        $division = $sortinfo[division_sort];
        $counter = 0;
        $tablewidth = floor(100 / $division);

        foreach ($subsort[$sortid] as $sort[sortid] => $sort[title]) {
                unset($subsortlistbit_level2);
                if ($counter == 0) {
                        $subsortlistbit_level1 .= "<tr bgcolor=\"$style[firstalt]\"}\">";
                } 
                $subsortlistbit_level1 .= "<td nowrap valign=\"top\" width=\"$tablewidth%\">";
                if (isset($subsort[$sort[sortid]])) {
                        unset($subsortlistbit_level3);
                        foreach($subsort[$sort[sortid]] as $childsort[sortid] => $childsort[title]) {
                                eval("\$subsortlistbit_level3 .= \"" . gettemplate('subsortlistbit_level3') . "\";");
                        } 
                        eval("\$subsortlistbit_level2 = \"" . gettemplate('subsortlistbit_level2') . "\";");
                } 
                eval("\$subsortlistbit_level1 .= \"" . gettemplate('subsortlistbit_level1') . "\";");
                $subsortlistbit_level1 .= "</td>\n";

                if (++$counter % $division == 0) {
                        $subsortlistbit_level1 .= "</tr>";
                        $counter = 0;
                } 
        } 
        if ($counter != 0) {
                for (;$counter < $division;$counter++) {
                        $subsortlistbit_level1 .= "<td></td>\n";
                } 
        } 
        eval("\$subsortlist = \"" . gettemplate('subsortlist') . "\";");
} 

unset($articlelist);

$perpage = $sortinfo[perpage];

$subsortids = getsubsorts($sortid);

$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM " . $db_prefix . "article AS article
                                      WHERE sortid IN (0" . $subsortids . ") AND visible=1");

$totalresults = $total[count];

$totalpages = ceil($total[count] / $perpage);

if ($pagenum < 1 OR empty($pagenum)) {
        $pagenum = 1;
} elseif ($pagenum > $totalpages) {
        $pagenum = $totalpages;
} 

$offset = ($pagenum-1) * $perpage;

if ($totalresults > 0) {
        $from = $offset + 1;
        if ($pagenum == $totalpages) {
                $to = $totalresults;
        } else {
                $to = $offset + $perpage;
        } 
} else {
        $from = 0;
        $to = 0;
} 

$articles = $DB->query("SELECT article.* FROM " . $db_prefix . "article AS article
                                     WHERE sortid IN (0" . $subsortids . ") AND visible=1
                                     ORDER BY date DESC
                                     LIMIT $offset,$perpage");

if ($DB->num_rows($articles) > 0) {
        $counter = 0;
        $row = 0;
        $division = $sortinfo[division_article];
        $tablewidth = floor(100 / $division);

        while ($article = $DB->fetch_array($articles)) {
                $article[time] = padate($timeformat_article, $article[date]);
                $article[date] = padate($dateformat_article, $article[date]);

                if (!empty($article[imageid])) {
                        // $article[img] = "<img src=\"showimg.php?iid=$article[imageid]\" border=\"0\" vspace=\"2\" hspace=\"2\" align=\"left\">";
                        eval("\$article[img] = \"" . gettemplate('sorthome_articlelistbit_img') . "\";");
                } else {
                        $article[img] = "";
                } 
                if ($counter == 0) {
                        if ($row++ % 2 == 0) {
                                $bgcolor = "$style[firstalt]";
                        } else {
                                $bgcolor = "$style[secondalt]";
                        } 
                        $articlelistbit .= "<tr bgcolor=\"$bgcolor\" align=\"center\">";
                } 
                $article[description] = str_replace("  ", "&nbsp;&nbsp;", nl2br($article[description]));
                $articlelistbit .= "<td nowrap valign=\"top\" width=\"$tablewidth%\">";
                eval("\$articlelistbit .= \"" . gettemplate('sorthome_articlelistbit') . "\";");

                $articlelistbit .= "</td>\n";

                if (++$counter % $division == 0) {
                        $articlelistbit .= "</tr>";
                        $counter = 0;
                } 
        } 
        if ($counter != 0) {
                for (;$counter < $division;$counter++) {
                        $articlelistbit .= "<td></td>\n";
                } 
        } 

        $pagelinks = makepagelink("$phparticleurl/sort.php/$sortid", $pagenum, $totalpages);
        eval("\$pagenav = \"" . gettemplate('pagenav') . "\";");

        eval("\$articlelist = \"" . gettemplate('sorthome_articlelist') . "\";");
} else {
        eval("\$articlelist = \"" . gettemplate('sorthome_articlelist_none') . "\";");
} 

unset($hotarticlelist);
$sortinfo[hotarticlenum] = intval($sortinfo[hotarticlenum]);
if ($sortinfo[hotarticlenum] > 0) {
        $articles = $DB->query("SELECT articleid,article.title,views FROM " . $db_prefix . "article AS article
                                           LEFT JOIN " . $db_prefix . "sort AS sort
                                             ON article.sortid=sort.sortid
                                           WHERE sort.sortid IN (0" . $subsortids . ") AND article.visible=1
                                           ORDER BY views DESC LIMIT $sortinfo[hotarticlenum]");
        while ($article = $DB->fetch_array($articles)) {
                eval("\$hotarticlelistbit .= \"" . gettemplate('hotarticlelistbit') . "\";");
        } 
        $DB->free_result($articles);
        eval("\$hotarticlelist = \"" . gettemplate('hotarticlelist') . "\";");
} 

unset($poparticlelist);
$sortinfo[ratearticlenum] = intval($sortinfo[ratearticlenum]);
if ($sortinfo[ratearticlenum] > 0) {
        $articles = $DB->query("SELECT articleid,article.title,views,(totalscore/voters) as averagescore,voters FROM " . $db_prefix . "article AS article
                                           LEFT JOIN " . $db_prefix . "sort AS sort
                                             ON article.sortid=sort.sortid
                                           WHERE voters>0 AND sort.sortid IN (0" . $subsortids . ") AND article.visible=1
                                           ORDER BY averagescore DESC
                                           LIMIT $sortinfo[ratearticlenum]");
        while ($article = $DB->fetch_array($articles)) {
                eval("\$poparticlelistbit .= \"" . gettemplate('poparticlelistbit') . "\";");
        } 
        $DB->free_result($articles);
        eval("\$poparticlelist = \"" . gettemplate('poparticlelist') . "\";");
} 

eval("dooutput(\"" . gettemplate('sorthome') . "\");");

?>